<?php
/*
PWision extension: Newspaper http://pw-newspaper.googlecode.com/
Copyright (C) 2012 Becheru Petru-Ioan

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
include_once("inc/PFormSection.inc");
include_once('inc/PTable.inc');
include_once("pw-newspaper.inc");
/** 	The Cite class is designed to model the section that allows a user to get the citation form for an article.
	*	\latexonly \label{PWision:Extensions:Newspaper:Cite} \index{Cite} \index{search} \index{TValue} \endlatexonly
	*	\brief cite an article.
	*	\version 1.1.17
	*/
class Cite extends PBasicFormSection
	{
// 	function getTitle()
// 		{
// 		return parent::getTitle().( $this->wasParamSet('kw') ?( ': '. $this->getParam('kw') ):'');
// 		}
	function setParams()
		{
		$this
			->addParam('articleID','/^[0-9]{0,10}$/i','')
			->addParam('form','/^[0-9]{0,3}$/i','')
			;
		}
	function if_not_all_parameters_are_set()
		{
		$this->ConstructLocation();
		parent::if_not_all_parameters_are_set();
		}
	function if_parameters_are_set()
		{
		$this->if_not_all_parameters_are_set();
		}
	function Head()
		{
		return
			parent::Head()
				->Insert(
					Object(new PSimpleTag("meta"))
						->setAtr("name","robots")
						->setAtr("content",'noindex')
					)
				;
		}
	/// constructor
	function Cite()
		{
		PBasicFormSection::PBasicFormSection();
		

		}
	function	Chapter2()
		{
		$ret=new PContent;

		$this->ArticleInfo=NewsPaper::getArticleInfo($this->getParam('articleID'));
		$URL='http://'.$_SERVER['HTTP_HOST'].'/('.$this->getParam('articleID').')';
		$pages=$this->ArticleInfo->firstPage.(($this->ArticleInfo->firstPage&&$this->ArticleInfo->lastPage)?'–':'').$this->ArticleInfo->lastPage;

		switch($this->getParam('form'))
			{
			default:
			case 1:// simple text
					$cit='';
					if($this->ArticleInfo->authors)
					$cit.=implode(", ",$this->ArticleInfo->authors).'. ';
					if($this->ArticleInfo->Title)
					$cit.=$this->ArticleInfo->Title.'. ';
					$cit.=Main()->getTitle();
					if($this->ArticleInfo->Volume)
					$cit.=', vol. '.$this->ArticleInfo->Volume;
					if($this->ArticleInfo->Issue)
					$cit.=', no. '.$this->ArticleInfo->Issue;
					if($pages)
					$cit.=', pp. '.$pages;
					if($this->ArticleInfo->Month)
					$cit.='. '.substr(pwision_newspaper_Article::$Month[(int)$this->ArticleInfo->Month],0,3).' '.$this->ArticleInfo->Day;
					if($this->ArticleInfo->Year)
					$cit.=', '.$this->ArticleInfo->Year;
					$cit.='. URL '.$URL;
					$cit.=' '.lang('accessed on').' '.date('Y-m-d');
					$ret->Insert($this->textInTable($cit,'text'));
				break;
			case 666://natural
				$T=new PTable();
					$T->setAtr('border','1');
// 					$T->setCaptionText(lang('all citation fields'));
					
					$T->AddCell()->Insert(PTextlang("ID"));
					$T->AddCell()->Insert(
							Sections::Link2(
								'Article',
								"",
								$this->ArticleInfo->ID,
								Object(new PURLAttributes)
									->setAtr('ID',$this->ArticleInfo->ID)
								)
						);
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("authors"));
					$authors=new PStructure(
						El('li'),
						El('ol')
						);
					if($this->ArticleInfo->authors)
					foreach($this->ArticleInfo->authors as $author)
						{
						$authors->newKid()
							->Insert(new PText($author))
							;
						}
					$T->AddCell()->Insert($authors);
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("Title"));
					$T->AddCell()->Insert(new PText($this->ArticleInfo->Title));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("journal name"));
					$T->AddCell()->Insert(new PText(Main()->getFullTitle()));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("short journal name"));
					$T->AddCell()->Insert(new PText(Main()->getTitle()));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("ISSN"));
					$T->AddCell()->Insert(new PText(Main()->getISSN()));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("address"));
					$T->AddCell()->Insert(new PText(Main()->getAddress()));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("volume"));
					$T->AddCell()->Insert(new PText($this->ArticleInfo->Volume));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("issue"));
					$T->AddCell()->Insert(new PText($this->ArticleInfo->Issue));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("Year"));
					$T->AddCell()->Insert(new PText($this->ArticleInfo->Year));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("Month"));
					$T->AddCell()->Insert(new PText($this->ArticleInfo->Month));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("Day"));
					$T->AddCell()->Insert(new PText($this->ArticleInfo->Day));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("Language"));
					$T->AddCell()->Insert(new PText(
						($this->ArticleInfo->Language=='')?Main()->getLanguage():$this->ArticleInfo->Language
						));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("first page"));
					$T->AddCell()->Insert(new PText($this->ArticleInfo->firstPage));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("last page"));
					$T->AddCell()->Insert(new PText($this->ArticleInfo->lastPage));
				$T->AddRow();
					$T->AddCell()->Insert(new PText("PDF URL"));
					$T->AddCell()->Insert(new PText($this->ArticleInfo->pdfURL));
				$T->AddRow();
					$T->AddCell()->Insert(new PText("Web URL"));
					$T->AddCell()->Insert(new PText($URL));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("keywords"));
					$keywords=new PStructure(
						El('li'),
						El('ul')
						);
					if($this->ArticleInfo->keywords)
					foreach($this->ArticleInfo->keywords as $keyword)
						{
						$keywords->newKid()
							->Insert(new PText($keyword))
							;
						}
					$T->AddCell()->Insert($keywords);
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("classifications"));
					$classifications=new PStructure(
						El('li'),
						El('ul')
						);
					if($this->ArticleInfo->classifications)
					foreach($this->ArticleInfo->classifications as $classification)
						{
						$classifications->newKid()
							->Insert(new PText($classification))
							;
						}
					$T->AddCell()->Insert($classifications);
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("Abstract title"));
					$T->AddCell()->Insert(new PText($this->ArticleInfo->absTitle));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("Abstract"));
					$T->AddCell()->Insert(new PText($this->ArticleInfo->textAbstract));
				$T->AddRow();
					$T->AddCell()->Insert(PTextlang("Abstract language"));
					$T->AddCell()->Insert(new PText(
						($this->ArticleInfo->absLanguage=='')?Main()->getLanguage():$this->ArticleInfo->absLanguage
						));
				$ret->Insert($T);
				break;
			case 2:
				$cit='<ref name="YOUR-CITATION-KEY">'."\n".'{{cite journal ';
				if($this->ArticleInfo->authors)
				$cit.="\n".' |author= [['.implode("]], [[",$this->ArticleInfo->authors ).']]';
				$cit.="\n".' |title= '.$this->ArticleInfo->Title;
				$cit.="\n".' |language= {{'.(($this->ArticleInfo->Language=='')?Main()->getLanguage():$this->ArticleInfo->Language).' icon}}';
				$cit.="\n".' |journal= [['.Main()->getFullTitle().'|'.Main()->getTitle().']]';
				$cit.="\n".' |issn= '.Main()->getISSN();
				if($this->ArticleInfo->Volume)
				$cit.="\n".' |volume= '.$this->ArticleInfo->Volume;
				if($this->ArticleInfo->Issue)
				$cit.="\n".' |issue= '.$this->ArticleInfo->Issue;
				if($pages)
				$cit.="\n".' |pages= '.$pages;
				$cit.="\n".' |year= '.$this->ArticleInfo->Year;
				$cit.="\n".' |month= '.substr(pwision_newspaper_Article::$Month[(int)$this->ArticleInfo->Month],0,3);
				$cit.="\n".' |url= '.$URL;
				$cit.="\n".' |id= '.$this->ArticleInfo->ID;
				$cit.="\n".' |format = HTML';
				$cit.="\n".' |accessdate= '.date('Y-m-d').' ';
				$cit.='}}(webpage has a translation button)'."\n".'</ref>';
				$ret->Insert($this->textInTable($cit,'Wikipedia'));
				break;
			case 3:
				$cit='@Article{YOUR-CITATION-KEY,';
				if($this->ArticleInfo->authors)
				$cit.="\n".'author={'.implode(" and ",$this->ArticleInfo->authors ).'},';
				$cit.="\n".'title={'.
					preg_replace('`([A-ZĂÎȘȚÂ]+)`','{$1}',$this->ArticleInfo->Title)
					.'},';
				$cit.="\n".'journal={'.Main()->getTitle().'},';
				$cit.="\n".'issn={'.Main()->getISSN().'},';
				$cit.="\n".'address={'.Main()->getAddress().'},';
				if($this->ArticleInfo->Volume)
				$cit.="\n".'volume={'.$this->ArticleInfo->Volume.'},';
				if($this->ArticleInfo->Issue)
				$cit.="\n".'number={'.$this->ArticleInfo->Issue.'},';
				if($pages)
				$cit.="\n".'pages={'.$pages.'},';
				$cit.="\n".'year={'.$this->ArticleInfo->Year.'},';
				if($this->ArticleInfo->Month)
				$cit.="\n".'month={'.substr(pwision_newspaper_Article::$Month[(int)$this->ArticleInfo->Month],0,3).' '.$this->ArticleInfo->Day.'},';
				$cit.="\n".'url={'.$URL.'},';
				$cit.="\n".'urldate={'.date('Y-m-d').'},';
				if($this->ArticleInfo->textAbstract)
				$cit.="\n".'abstract={'.$this->ArticleInfo->textAbstract.'},';
				if($this->ArticleInfo->keywords)
				$cit.="\n".'keywords={'.implode(", ",$this->ArticleInfo->keywords ).'},';
				$cit.="\n".'}';
				$ret->Insert($this->textInTable($cit,'BibTeX'));
				break;
			case 4:
				$cit='TY  - JOUR';
				$cit.="\n".'JO  - '.Main()->getFullTitle();
				$cit.="\n".'J2  - '.Main()->getTitle();
				$cit.="\n".'CY  - '.Main()->getAddress();
				$cit.="\n".'SN  - '.Main()->getISSN();
				if($this->ArticleInfo->Volume)
				$cit.="\n".'VL  - '.$this->ArticleInfo->Volume;
				if($this->ArticleInfo->Issue)
				$cit.="\n".'IS  - '.$this->ArticleInfo->Issue;
				$cit.="\n".'TI  - '.$this->ArticleInfo->Title;
				$cit.="\n".'LA  - '.$this->expandLanguageCode(($this->ArticleInfo->Language=='')?Main()->getLanguage():$this->ArticleInfo->Language);
				if($this->ArticleInfo->absTitle)
				$cit.="\n".'TT  - '.$this->ArticleInfo->absTitle;
				if($this->ArticleInfo->textAbstract)
				$cit.="\n".'AB  - '.
					preg_replace(
						'`[[:space:]]+`',' ',
						$this->ArticleInfo->textAbstract
						)
					;
				if($this->ArticleInfo->authors)
					foreach($this->ArticleInfo->authors as $author)
						$cit.="\n".'A1  - '.$author;
				$cit.="\n".'DA  - '.$this->ArticleInfo->Year.'/'.$this->ArticleInfo->Month.'/'.$this->ArticleInfo->Day;
				if($this->ArticleInfo->firstPage)
				$cit.="\n".'SP  - '.$this->ArticleInfo->firstPage;
				if($this->ArticleInfo->lastPage)
				$cit.="\n".'EP  - '.$this->ArticleInfo->lastPage;
				$cit.="\n".'UR  - '.$URL;
				if($this->ArticleInfo->keywords)
					foreach($this->ArticleInfo->keywords as $keyword)
						$cit.="\n".'KW  - '.$keyword;
				$cit.="\n".'ER  - '."\n";
				$ret->Insert($this->textInTable($cit,'RIS'));
				break;
			case 5:// https://www.facebook.com/sharer.php?u=http%3A%2F%2Ftinyurl.com%2F7vq4o8g
				$ret->Insert(
					El('a')
							->setAtr('href','https://www.facebook.com/sharer.php?u='.urlencode($URL))
							->setAtr('target','_blank')
						->Insert(
							El('span')
									->setAtr('class','fbShare')
								->Insert(PTextlang('Click here to post on Facebook'))
							)
					);
				break;
			case 6:// //https://m.google.com/app/plus/x/?v=compose&content=http%3A%2F%2Ftinyurl.com%2F7vq4o8g
				$ret->Insert(
					El('a')
							->setAtr('href','https://plus.google.com/share?url='.urlencode($URL))
							->setAtr('target','_blank')
						->Insert(PTextlang('Click here to post on Google'))
					);
				break;
			case 7://RePEc
				$ret->Insert($this->textInTable($this->ArticleInfo->RePEc(),'RePEc'));
				break;
			case 8://DOAJ
				$ret->Insert($this->textInTable($this->ArticleInfo->DOAJ(),'DOAJ'));
				break;
			}
		$select=Array2Select(
					'form',
					array(
						1 => 'simple text',
						2 => 'Wikipedia',
						6 => 'Google +',
						5 => 'Facebook',
						3 => 'BibTeX',
						4 => 'RIS',
						7 => 'RePEc RDF',
						8 => 'DOAJ XML',
						666 => 'all citation fields'
						),
					$this->getParam('form'),
					"select a citation form"
					);
		$select
			->getContainer()	
					->setAtr('onchange','submit()')
					->setAtr('size','4')
					;
		$this->TheForm
				->setAtr('method','post')
			->Insert( new PInput("hidden",$this->getParam('articleID'),'articleID') )
			->Insert(
				Object(
					new PFieldSet(
						Object(new PContent)
							->Insert($select)
// 							->Insert( new PInput("submit",lang("Show!"),"") )
						)
					)
						->setAtr('class','Cite')
					->Insert(
						Object(new PFieldSet(PTextlang('description')))
							->Insert(
								PTextlang('Cite'.$this->getParam('form'))
								)
						)
					->Insert(sEl('br'))
					->Insert($ret)
				)
			;
		return parent::Chapter2();
		}
	function textInTable($text,$caption)
		{
		$T=new PTable();
// 			$T->setCaptionText(lang($caption));
			$T->AddCell()->Insert(new PText(
				TextArea("", $text , 15)
							->setAtr('cols','80')
					->setAtr('readonly','readonly')
					->setAtr('title',lang('Select, copy and paste this text'))
				));
		return $T;
		}
	function expandLanguageCode($code)
		{
		$table= array(// http://www.w3schools.com/tags/ref_language_codes.asp
			'ro'=> 'Romanian',
			'la'=> 'Latin',
			'en' => 'English',
			'fr' => 'French',
			'de' => 'German',
			'es' => 'Spanish'
			);
		return $table[$code];
		}
	}
?>